#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
#include <game/game.h>
#include <QPainter>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui       -> setupUi(this);
    gameField = new FieldWidget(this);
    timer     = new QTimer(this);
    connect(timer, SIGNAL(timeout()), gameField, SLOT(tick()));

    Game::initGame();
    controller = new TKeyboardController(Game::Instance() -> getUserObject());

    thread = new TClientThread(this);

    connect(thread,SIGNAL(finished()), this, SLOT(connectionLost()), Qt::DirectConnection);
    //connect(timer, SIGNAL(timeout()), thread, SLOT(exchangeData()),Qt::DirectConnection);

    timer    -> start(50);
    thread   -> start();
}

MainWindow::~MainWindow(){
    delete ui;
    delete timer;
    delete gameField;
    delete controller;
    delete thread;
}

void MainWindow::keyPressEvent(QKeyEvent * event){
    controller -> control(event->key());
}

void MainWindow::connectionLost(){
    exit(0);
}
